import WebSocketComponent from "../WebSocket/WebSocketComponent";

export default abstract class RequestHandler<TRequest extends Chaos.IRequest | Chaos.IActorRequest, TResponse extends (Chaos.IResponse | Chaos.IActorResponse) & { Opcode: number }> implements Chaos.IMessageHandler {
    public async HandleAsync(request: TRequest): Promise<void> {
        const response = this.CreateResponse();
        response.RpcId = request.RpcId;

        this.RunAsync(request, response, WebSocketComponent.Default.Reply.bind(WebSocketComponent.Default));
    }

    protected abstract CreateResponse(): TResponse;
    protected abstract RunAsync(request: TRequest, response: TResponse, reply: (response: TResponse) => void): Promise<void>;
}